<?php

class BudgetController extends Controller
{
    /*
    * 添加
    *
    */
    public function actionAddCase()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $inputinfo = Util::getParamNotEmpty("inputinfo");





            //输入参数验证



            //添加
            $obj = new BudgetCase();
            Util::initObjData($obj);


            $obj->user_id = $user_id;
            $obj->input_info = $inputinfo;
            $obj->status = MsgConst::BUDGET_STATUS_INIT;
            //$obj->input_desc = $remark;
           // $obj->result_info = BudgetService::generateRestuleInfo($obj);

            $obj = BudgetService::generateRestuleInfo($obj,-1);


            $obj->result_info = json_encode(  $obj->result_info);
            Util::saveObjData($obj);


            $case_info = BudgetService::generateRestuleInfo($obj,$obj->id);


            //返回数据
            //Util::responseMsg(MsgConst::RETURN_SUCCESS);
            Util::responseObj($case_info);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }


    /*
   * 修改
   *
   */
    public function actionupdateCase()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            $case_id = Util::getParamNotEmpty("case_id");

            //参数验证
            $inputinfo = Util::getParamNotEmpty("inputinfo");





            //输入参数验证

            $obj =  BudgetCase::model()->findByPK($case_id);

            Util::checkExistAndNotDelete($obj);

            //验证用户
            if($obj->user_id != $user_id)
                throw new Exception("不能修改别人的案例");



            //添加

            $obj->input_info = $inputinfo;
            $obj->status = MsgConst::BUDGET_STATUS_INIT;
            //$obj->input_desc = $remark;
            // $obj->result_info = BudgetService::generateRestuleInfo($obj);

            $obj = BudgetService::generateRestuleInfo($obj,-1);


            $obj->result_info = json_encode(  $obj->result_info);
            Util::saveObjData($obj);


            $case_info = BudgetService::generateRestuleInfo($obj,$obj->id);


            //返回数据
            //Util::responseMsg(MsgConst::RETURN_SUCCESS);
            Util::responseObj($case_info);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 添加案例详情
    *
    */
    public function actionaddCaseDetail()
    {

        //事务开始
        $connection = Yii::app()->db;

        $transaction = $connection->beginTransaction();



        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $case_id = Util::getParamNotEmpty("case_id");
            $type_id = Util::getParamNotEmpty("type_id");
            $index = Util::getParamNotEmpty("index");
            $length = Util::getParamNotEmpty("length");
            $width = Util::getParamNotEmpty("width");
            $height = Util::getParamNotEmpty("height");
            $ground_typeid = Util::getParamNotEmpty("ground_typeid");
            $wall_typeid = Util::getParamNotEmpty("wall_typeid");


            BudgetService::isVaildGroundMaterial($ground_typeid);
            BudgetService::isVaildWallMaterial($wall_typeid);


            //案例
            $case = BudgetCase::model()->findByPK($case_id);
            Util::checkExistAndNotDelete($case);

            //种类
            $type = BudgetHousetype::model()->findByPK($type_id);
            Util::checkExistAndNotDelete($type);

            $inputinfo = $case->input_info;
            $input_array = json_decode($inputinfo);

            $num = 0;
            for($i=0;$i<count($input_array);$i++)
            {
                if($input_array[$i]->type_id == $type_id)
                {
                    $num = $input_array[$i]->num;
                    break;
                }

            }

            //index 必须小于总个数
            if($index > ($num-1) )
            {
                throw new Exception($type->name."的数量最多为".$num."个");
            }


            //查找该案例是否已经有数据
            $searchsql = Util::initSearchSql();

            $searchsql = Util::getSerarchStrAdd($searchsql,"case_id",$case_id);
            $searchsql = Util::getSerarchStrAdd($searchsql,"index_num",$index);
            $searchsql = Util::getSerarchStrAdd($searchsql,"house_type_id",$type_id);


            $obj = BudgetCaseDetail::model()->find($searchsql);


            if(empty($obj)==true)
            {
                //添加
                $obj = new BudgetCaseDetail();
                Util::initObjData($obj);
                $obj->case_id = $case_id;
                $obj->house_type_id = $type_id;
                $obj->index_num = $index;

            }
            else
            {

            }

            $obj->length = $length;
            $obj->width = $width;
            $obj->height = $height;
            $obj->ground_type_id = $ground_typeid;
            $obj->wall_type_id = $wall_typeid;
            $obj->house_type = $type->name;

            //地面材料
           $material_ground =   BudgetMaterial::model()->findByPK($obj->ground_type_id);
            Util::checkExistAndNotDelete($material_ground);

            //墙面材料
            $material_wall =   BudgetMaterial::model()->findByPK($obj->wall_type_id);
            Util::checkExistAndNotDelete($material_wall);

            $obj->ground_type_name = $material_ground->name;
            $obj->wall_type_name = $material_wall->name;



            Util::saveObjData($obj);


            //查看更新状态
            BudgetService::checkCaseStatus($case);



            $transaction->commit();//事务结束
            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {

            $transaction->rollback();//回滚函数
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 获取列表
    *
    */
    public function actionGetCase()
    {
        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();

            $case_id = Util::getParam("case_id",-1);


            //有 case——id

            if($case_id == -1)
            {
                $case_id = BudgetService::getCaseIdByUserid($user_id);

                if($case_id < 0 )
                    throw new Exception("案例id不存在");

            }


            $case_info = BudgetCase::model()->findByPK($case_id);
            Util::checkExistAndNotDelete($case_info);


            $case_info = BudgetService::generateRestuleInfo($case_info,$case_info->id);




            Util::responseObj($case_info);









            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 获取列表
    *
    */
    public function actiongetmateriallist()
    {
        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();

            $case_id = Util::getParam("case_id",-1);



            //无 case——id

            if($case_id == -1)
            {
                $case_id = BudgetService::getCaseIdByUserid($user_id);

                if($case_id < 0 )
                    throw new Exception("案例id不存在");

            }

            $case_info = BudgetCase::model()->findByPK($case_id);
            Util::checkExistAndNotDelete($case_info);
            $list=BudgetService::getMaterialList($case_info);


                //返回数据
                Util::responseList(count($list), $list);


            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }


    /*
   * 计算表达式
   *
   */
    public function actioncalcformula()
    {
        try {
            //参数验证

            //检测
           // $user_id = SessionService::getUserIDBySessionid();

            $formula = Util::getParamNotEmpty("formula");


            //中缀表达式－>后缀表达式
           // $out = BudgetFormulaService::trans_exp($formula);


          //  $formula = '9 + ( 3 - 1 ) * 3 + 10 / 2';

          //  $formula = '3 + 10 - 5 * 2';


            $newStrList = BudgetFormulaService::getPostExp($formula);

            //echo $newStrList;

           $value =   BudgetFormulaService::getValueByPostExp($newStrList);






            //返回数据
            Util::responseObj($value);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }










}